import defaultSettings from '@/settings'
import Cookies from 'js-cookie'

const { showSettings, fixedHeader, sidebarLogo, defaultTheme } = defaultSettings

const state = {
  showSettings: showSettings,
  fixedHeader: fixedHeader,
  sidebarLogo: sidebarLogo,
  theme: Cookies.get('theme') || defaultTheme
}

const mutations = {
  CHANGE_SETTING: (state, { key, value }) => {
    // eslint-disable-next-line no-prototype-builtins
    if (state.hasOwnProperty(key)) {
      state[key] = value
    }
    if (key === 'theme') {
      Cookies.set('theme', value)
    }
  },
  resetTheme(state) {
    // 把主题设置为默认的主题
    state.theme = defaultTheme
    // 移除cookie中的theme
    Cookies.remove('theme')
  }
}

const actions = {
  changeSetting({ commit }, data) {
    commit('CHANGE_SETTING', data)
  }
}

export default {
  namespaced: true,
  state,
  mutations,
  actions
}

